<template>
  <div class="goods-comment" v-if="commentInfo">
    <div class="head">
      <div class="data">
        <p>
          <span>{{ commentInfo.salesCount }}</span
          ><span>人购买</span>
        </p>
        <p>
          <span>{{ commentInfo.praisePercent }}</span
          ><span>好评率</span>
        </p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说：</div>
        <div class="dd">
          <a
            href="javascript:;"
            v-for="item in commentInfo.tags"
            :key="item.title"
            @click="changeFilter(item.title)"
            :class="{ active: activeTitle === item.title }"
          >
            {{ item.title }}（{{ item.tagCount }}）
          </a>
        </div>
      </div>
    </div>
    <div class="sort">
      <span>排序：</span>
      <a
        @click="data.sortField = null"
        :class="{ active: data.sortField === null }"
        href="javascript:;"
      >
        默认
      </a>
      <a
        @click="data.sortField = 'createTime'"
        :class="{active:data.sortField ==='createTime'}"
        href="javascript:;"
      >
        最新
      </a>
      <a
        @click="data.sortField = 'praiseCount'"
        :class="{active:data.sortField ==='praiseCount'}"
        href="javascript:;"
      >
        最热
      </a>
    </div>
    <!-- 评论列表 -->
    <div class="list">
      <div class="item" v-for="(item, index) in commentList" :key="index">
        <div class="user">
          <img :src="item.member.avatar" alt="" />
          <span>{{ nickNameHide(item.member.nickname) }}</span>
        </div>
        <div class="body">
          <div class="score">
            <i v-for="i in item.score" :key="i" class="iconfont icon-wjx01"></i>
            <i
              v-for="i in 5 - item.score"
              :key="i"
              class="iconfont icon-wjx02"
            ></i>
            <span class="attr">
              {{ specsText(item.orderInfo.specs) }}
            </span>
          </div>
          <div class="text">
            {{ item.content }}
          </div>
          <GoodsCommentImage
            :pictures="item.pictures"
            v-if="item.pictures.length"
          ></GoodsCommentImage>
          <div class="time">
            <span>{{ item.createTime }}</span>
            <span class="zan">
              <i class="iconfont icon-dianzan"></i>{{ item.praiseCount }}
            </span>
          </div>
        </div>
      </div>
    </div>
    <XtxPagination
      v-if="total"
      :total="total"
      :current-page="currentPage"
      :page-size="data.pageSize"
      @current-change="getCurrentPage"
    />
  </div>
</template>

<script>
import { findGoodsCommentInfo, findGoodsCommentList } from '@/api/goods'
import { inject, reactive, ref, watch } from 'vue'
import GoodsCommentImage from './goods-comment-image.vue'

const nickNameHide = nickName => {
  return nickName.slice(0, 1) + '****' + nickName.slice(-1)
}

const specsText = specs => {
  return specs.map(item => `${item.name}：${item.nameValue}`).join(' ')
}

export default {
  components: { GoodsCommentImage },
  name: 'GoodsComment',
  setup () {
    const goods = inject('goods')
    // console.log(goods.value.id)
    const commentInfo = ref(null)
    const id = goods.value.id

    findGoodsCommentInfo(id).then(({ result }) => {
      // console.log(result)
      // 处理一下 tags 数据
      result.tags = [
        { title: '全部评价', tagCount: result.evaluateCount },
        { title: '有图', tagCount: result.hasPictureCount },
        ...result.tags
      ]
      // 处理完毕后，再赋值给响应式数据
      commentInfo.value = result
    })

    const activeTitle = ref('全部评价')

    // 评论列表
    const commentList = ref([])
    // 评论总条数
    const total = ref(0)
    // 当前页码
    const currentPage = ref(1)

    const data = reactive({
      // page: 1, // 是 1 页码
      pageSize: 10, // 是 10 页尺寸
      hasPicture: null, // 否 true 是否有图
      tag: null, // 否 质量好 标签
      sortField: null // 否 praiseCount 排序字段，可选值范围[praiseCount,createTime]
      // sortMethod: '' // 否 desc 排序方法，可选值范围[asc,desc],默认为desc
    })

    const getList = () => {
      findGoodsCommentList(id, { ...data, page: currentPage.value }).then(
        ({ result }) => {
          console.log(result)
          commentList.value = result.items
          total.value = result.counts
        }
      )
    }

    // 点击改变筛选参数
    const changeFilter = tag => {
      // console.log(tag)
      // 高亮效果
      activeTitle.value = tag
      // 请求参数 hasPicture 和 tag 参数处理
      if (tag === '有图') {
        data.hasPicture = true
        data.tag = null
      } else if (tag === '全部评价') {
        data.hasPicture = null
        data.tag = null
      } else {
        data.hasPicture = null
        data.tag = tag
      }
      // currentPage.value = 1
      // 重置排序规则
      data.sortField = null
    }

    watch(
      data,
      () => {
        // 页码重置
        currentPage.value = 1
        // 获取评价分页数据
        getList()
      },
      { immediate: true }
    )

    const getCurrentPage = val => {
      // console.log('当前子组件的页面为', val)
      currentPage.value = val
      getList()
    }

    return {
      commentInfo,
      activeTitle,
      commentList,
      nickNameHide,
      specsText,
      getCurrentPage,
      total,
      currentPage,
      data,
      changeFilter
    }
  }
}
</script>

<style scoped lang="less">
.goods-comment {
  .list {
    padding: 0 20px;
    .item {
      display: flex;
      padding: 25px 10px;
      border-bottom: 1px solid #f5f5f5;
      .user {
        width: 160px;
        img {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          overflow: hidden;
        }
        span {
          padding-left: 10px;
          color: #666;
        }
      }
      .body {
        flex: 1;
        .score {
          line-height: 40px;
          .iconfont {
            color: #ff9240;
            padding-right: 3px;
          }
          .attr {
            padding-left: 10px;
            color: #666;
          }
        }
      }
      .text {
        color: #666;
        line-height: 24px;
      }
      .time {
        color: #999;
        display: flex;
        justify-content: space-between;
        margin-top: 5px;
      }
    }
  }
  .head {
    display: flex;
    padding: 30px 0;
    .data {
      width: 340px;
      display: flex;
      padding: 20px;
      p {
        flex: 1;
        text-align: center;
        span {
          display: block;
          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }
          &:last-child {
            color: #999;
          }
        }
      }
    }
    .tags {
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;
      .dt {
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }
      .dd {
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        > a {
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          border: 1px solid #e4e4e4;
          background: #f5f5f5;
          color: #999;
          text-align: center;
          line-height: 40px;
          &:hover {
            border-color: @xtxColor;
            background: lighten(@xtxColor, 50%);
            color: @xtxColor;
          }
          &.active {
            border-color: @xtxColor;
            background: @xtxColor;
            color: #fff;
          }
        }
      }
    }
  }
  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    > span {
      margin-left: 20px;
    }
    > a {
      margin-left: 30px;
      &.active,
      &:hover {
        color: @xtxColor;
      }
    }
  }
}
</style>
